//
//  ZYXUserNetManager.m
//  ZYXApp
//
//  Created by 梁炜东 on 2017/4/24.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "ZYXUserNetManager.h"
#import "ZYXNetManager.h"

#define passwordSalt @"Random_MD5_SALT_QSCGYJL"//警告 ⚠️！！！ 盐值任何人任何时候不得更改 --add by梁炜东

@implementation ZYXUserNetManager
+(ZYXUser *)user
{
    NSData *data = [UserDefaults objectForKey:UserArchive];
    ZYXUser *user = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return user;
}
+(void)removeUser
{
    [UserDefaults removeObjectForKey:UserArchive];
    [UserDefaults synchronize];
}
//注册发送验证码
+(void)userRegisterAuthAccount:(NSString *)account Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    [KeyWindow  makeToastActivity];
    NSDictionary *dict = @{@"phone" : account,};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(kUserAuthModifPw).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger status = [dic[@"status"] integerValue];
         if (!error)
         {
             successBlock(dict, status);
         }else
         {
             failBlock(error);
         }
     }];
}
//注册
+(void)userRegisterAccount:(NSString *)account Auth:(NSString *)auth Pw:(NSString *)password Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    [KeyWindow makeToastActivity];
    NSString *pw = [YTTools md5:[NSString stringWithFormat:@"%@%@", password, passwordSalt]];//进行加盐加密操作
    NSDictionary *dict = @{@"phone" : account,
                           @"password" : pw,
                           @"code" : auth};
    
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(kUserRegist).bh_responseSerializer(HTTPResponseSerializer).bh_requestSerializer(HTTPRequestSerializer).bh_ContentType(Form_urlencoded).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         if (!error)
         {
             NSString *strHtml = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
             NSDictionary *dataDic = [NSDictionary parseJSONStringToNSDictionary:strHtml];
             NSLog(@"HTML = %@",strHtml);
             NSInteger status = [dataDic[@"status"] integerValue];
             successBlock(dataDic[@"data"], status);
         }else
         {
             failBlock(error);
         }
     }];
}

+(void)userLoginAccount:(NSString *)account Pw:(NSString *)password Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    [KeyWindow makeToastActivity];
    NSString *pw = [YTTools md5:[NSString stringWithFormat:@"%@%@", password, passwordSalt]];
    NSDictionary *dict = @{@"phone" : account,
                           @"password" : pw};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(kUserLogin).bh_responseSerializer(JSONResponseSerializer).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
    {
        [KeyWindow hideToastActivity];
        NSDictionary *dic = (NSDictionary *)response;
        NSInteger status = [dic[@"status"] integerValue];
        if (!error)
        {
            if (status == 2000)
            {
                NSError *error = nil;
                ZYXUser *user = [[ZYXUser alloc] initWithDictionary:dic[@"data"] error:&error];
                NSAssert(!error, @"user有错误出现");
                NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
                [UserDefaults setObject:userData forKey:UserArchive];
                [UserDefaults synchronize];
            }
            successBlock(dic[@"datra"], status);
        }else
        {
            failBlock(error);
        }
    }];
}

+(void)userModifSendAuthAccount:(NSString *)account Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    [KeyWindow makeToastActivity];
    NSDictionary *dict = @{@"phone" : account};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(kUserAuthModifPw).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
    {
        [KeyWindow hideToastActivity];
        NSDictionary *dic = (NSDictionary *)response;
        NSInteger status = [dic[@"status"] integerValue];
        if (!error)
        {
            successBlock(dict, status);
        }else
        {
            failBlock(error);
        }
    }];
}
+(void)userModifAccount:(NSString *)account Auth:(NSString *)auth Pw:(NSString *)password Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    [KeyWindow makeToastActivity];
    NSString *pw = [YTTools md5:[NSString stringWithFormat:@"%@%@", password, passwordSalt]];
    NSDictionary *dict = @{@"phone" : account,
                           @"password" : pw,
                           @"code" : auth};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(kUserForgetPw).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger status = [dic[@"status"] integerValue];
         if (!error)
         {
             successBlock(dict, status);
         }else
         {
             failBlock(error);
         }
     }];
}
//修改密码--
+(void)userChangeNewPw:(NSString *)newPw OldPw:(NSString *)oldPw Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    ZYXUser *user = [ZYXUserNetManager user];
    if (!user || !user.token || [user.token isEqualToString:@""])
    {
        successBlock(nil, 1055);
        return;
    }
    [KeyWindow makeToastActivity];
    NSString *old = [YTTools md5:[NSString stringWithFormat:@"%@%@", oldPw, passwordSalt]];//进行加盐加密操作
    NSString *new = [YTTools md5:[NSString stringWithFormat:@"%@%@", newPw, passwordSalt]];//进行加盐加密操作
    NSDictionary *dict = @{@"token" : user.token,
                           @"phone" : user.phone,
                           @"oldpwd" : old,
                           @"newpwd" : new};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(kUserChangePw).bh_responseSerializer(JSONResponseSerializer).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger status = [dic[@"status"] integerValue];
         if (!error)
         {
             successBlock(dict, status);
         }else
         {
             failBlock(error);
         }
     }];
}
+(void)userInfoUpdateSex:(NSString *)sex Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    ZYXUser *user = [ZYXUserNetManager user];
    if (!user || !user.token || [user.token isEqualToString:@""])
    {
        successBlock(nil, 1055);
        return;
    }
    [KeyWindow makeToastActivity];
    NSDictionary *dict = @{@"token" : user.token,
                           @"sex" : sex};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(KUserInfoModify).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         NSLog(@"");
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger status = [dic[@"status"] integerValue];
         if (!error)
         {
             ZYXUser *user = [ZYXUserNetManager user];
             user.sex = dic[@"data"][@"sex"];
             NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
             [UserDefaults setObject:userData forKey:UserArchive];
             [UserDefaults synchronize];
             successBlock(dict, status);
         }else
         {
             failBlock(error);
         }
     }];
}

+(void)userInfoUpdateNickname:(NSString *)nickname Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    ZYXUser *user = [ZYXUserNetManager user];
    if (!user || !user.token || [user.token isEqualToString:@""])
    {
        successBlock(nil, 1055);
        return;
    }
    [KeyWindow makeToastActivity];
    NSDictionary *dict = @{@"token" : user.token,
                           @"nickname" : nickname};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(KUserInfoModify).bh_responseSerializer(JSONResponseSerializer).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
    {
        [KeyWindow hideToastActivity];
        NSDictionary *dic = (NSDictionary *)response;
        NSInteger status = [dic[@"status"] integerValue];
        if (!error)
        {
            ZYXUser *user = [ZYXUserNetManager user];
            user.nickname = dic[@"data"][@"nickname"];
            NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
            [UserDefaults setObject:userData forKey:UserArchive];
            [UserDefaults synchronize];
            successBlock(dict, status);
        }else
        {
            failBlock(error);
        }
    }];
}

+(void)userInfoUpdateIntroduce:(NSString *)introduce Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    
    ZYXUser *user = [ZYXUserNetManager user];
    if (!user || !user.token || [user.token isEqualToString:@""])
    {
        successBlock(nil, 1055);
        return;
    }
    [KeyWindow makeToastActivity];
    NSDictionary *dict = @{@"token" : user.token,
                           @"id" : user.Id,
                           @"introduce" : introduce};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(KUserInfoModify).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         NSLog(@"");
     }];
}
+(void)userInfoUpdateBirthday:(NSString *)birthday Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    ZYXUser *user = [ZYXUserNetManager user];
    if (!user || !user.token || [user.token isEqualToString:@""])
    {
        successBlock(nil, 1055);
        return;
    }
    [KeyWindow makeToastActivity];
    NSDictionary *dict = @{@"token" : user.token,
                           @"birthday" : birthday};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(KUserInfoModify).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger status = [dic[@"status"] integerValue];
         if (!error)
         {
             if (status == 2000)
             {
                 ZYXUser *user = [ZYXUserNetManager user];
                 user.birthday = dic[@"data"][@"birthday"];
                 NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
                 [UserDefaults setObject:userData forKey:UserArchive];
                 [UserDefaults synchronize];
             }
             successBlock(dict, status);
         }else
         {
             failBlock(error);
         }
     }];
}

//意见反馈
+(void)userFeedBack:(NSString *)title Content:(NSString *)content Success:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    ZYXUser *user = [ZYXUserNetManager user];
    if (!user || !user.token || [user.token isEqualToString:@""])
    {
        successBlock(nil, 1055);
        return;
    }
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    //获取app版本
    NSString *app_version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    //获取app build版本
    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    NSDictionary *dict = nil;
//    NSString *softVersion = [UserDefaults objectForKey:FeedBackNeedSoftVersion];//当前云台没调好，这个固件版本号置为空--add by lwd 2017-11-28
    NSString *softVersion = nil;
    if (softVersion && ![softVersion isEqualToString:@""])
    {
        dict = @{@"token" : user.token,
                 @"title" : title,
                 @"content" : content,
                 @"rom_ver" : softVersion,
                 @"app_platform" : @"iOS",
                 @"app_ver" : [NSString stringWithFormat:@"%@(%@)", app_version, app_build]};
    }else
    {
        dict = @{@"token" : user.token,
                 @"title" : title,
                 @"content" : content,
                 @"app_platform" : @"iOS",
                 @"app_ver" : [NSString stringWithFormat:@"%@(%@)", app_version, app_build]};
    }
    [KeyWindow makeToastActivity];
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(kUserFeedBack).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger status = [dic[@"status"] integerValue];
         if (!error)
         {
             successBlock(dict, status);
         }else
         {
             failBlock(error);
         }
     }];
}
//获取用户信息
+(void)userGetUserInfoSuccess:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    
    ZYXUser *user = [ZYXUserNetManager user];
    if (!user || !user.token || [user.token isEqualToString:@""])
    {
        successBlock(nil, 1055);
        return;
    }
    [KeyWindow  makeToastActivity];
    NSDictionary *dict = @{@"token" : user.token};
    [[ZYXNetManager sharedManager].bh_requestType(POST).bh_baseUrl(BaseUrl).bh_requestUrl(kUserGetUserInfo).bh_parameters(dict) startRequestWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
         [KeyWindow hideToastActivity];
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger status = [dic[@"status"] integerValue];
         if (!error)
         {
             if (status == 2000)
             {
                 NSError *error = nil;
                 ZYXUser *user = [[ZYXUser alloc] initWithDictionary:dic[@"data"] error:&error];
                 NSAssert(!error, @"user有错误出现");
                 NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
                 [UserDefaults setObject:userData forKey:UserArchive];
                 [UserDefaults synchronize];
             }
             successBlock(dict, status);
         }else
         {
             failBlock(error);
         }
     }];
}
+(void)uploadImage:(UIImage *)image block:(UserSuccessBlock)successBlock Fail:(UserFailBlock)failBlock
{
    int net = [YTTools getNetState];
    if (net == 0)
    {
        [KeyWindow makeToast:Language(@"请连接网络")];
        return;
    }
    ZYXUser *user = [ZYXUserNetManager user];
    if (!user || !user.token || [user.token isEqualToString:@""])
    {
        successBlock(nil, 1055);
        return;
    }
    NSData *data = UIImageJPEGRepresentation(image, 1);
    NSDictionary *dict = @{@"token" : user.token,
                           @"img" : data};
     [KeyWindow  makeToastActivity];
    [[ZYXNetManager sharedManager].bh_baseUrl(BaseUrl).bh_requestUrl(kUploadImage).bh_parameters(dict).bh_ImageData(data).bh_requestSerializer(JSONRequestSerializer).bh_responseSerializer(HTTPResponseSerializer) startImageUploadWithCompleteHandler:^(NSURLSessionDataTask *task, id response, NSError *error)
     {
          [KeyWindow  hideToastActivity];
         NSDictionary *dic = (NSDictionary *)response;
         NSInteger status = [dic[@"status"] integerValue];
         if (!error)
         {
             NSString *urlStr = response[@"data"][@"thumb_path"];
             user.photopath = urlStr;
             NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:user];
             [UserDefaults setObject:userData forKey:UserArchive];
             [UserDefaults synchronize];
            successBlock(dict, status);
         }else
         {
             failBlock(error);
         }
     }];
}
@end
